package com.truongnha.clienttn.presentation.temp;


import java.util.LinkedList;
import java.util.List;

import org.apache.http.cookie.Cookie;
import org.apache.http.impl.cookie.BasicClientCookie;

import android.util.Log;

/**
 * Handles converting Cookies to strings to save them persistently across runs.
 */
public class CookieFactory {
  
  private static final String SEPERATOR = ";";
  private static final String COOKIE_SEPERATOR = ";-;";
  
  private static final String[] ATTRIB_KEYS = new String[]{
      BasicClientCookie.COMMENT_ATTR,
      BasicClientCookie.COMMENTURL_ATTR,
      BasicClientCookie.DISCARD_ATTR,
      BasicClientCookie.DOMAIN_ATTR,
      BasicClientCookie.EXPIRES_ATTR,
      BasicClientCookie.MAX_AGE_ATTR,
      BasicClientCookie.PATH_ATTR,
      BasicClientCookie.PORT_ATTR,
      BasicClientCookie.SECURE_ATTR,
      BasicClientCookie.VERSION_ATTR
    };
  
  /**
   * Expects a String like
   *   namevalue...
   * 
   * At least name and value must be set.
   */
  public static BasicClientCookie loadCookieFromString(String cookieString) {
    Log.d("MDE", "Loading cookies from String: " + cookieString);
    String[] strings = cookieString.split(SEPERATOR);
    BasicClientCookie retVal = new BasicClientCookie(strings[0].replaceAll("\\\"" + SEPERATOR, SEPERATOR), strings[1].replaceAll("\\\"" + SEPERATOR, SEPERATOR));
    //Log.d("MDE", "Cookie has " + strings.length + " parts");
    //Log.d("MDE", "attribs has " + ATTRIB_KEYS.length + " parts");
    for (int i = 0; i < ATTRIB_KEYS.length && (i + 2) < strings.length; i++) {
      String key = ATTRIB_KEYS[i];
      String val = strings[i+2];
      if (val != null && val.length() > 0) {
        retVal.setAttribute(key, val);
      }
    }
    
    return retVal;
  }
  
  public static List<Cookie> loadCookiesFromString(String cookies) {
    List<Cookie> cookieList = new LinkedList<Cookie>();
    
    String[] cookieArr = cookies.split(COOKIE_SEPERATOR);
    for (String c : cookieArr) {
      cookieList.add(loadCookieFromString(c));
    }
    
    return cookieList;
  }
  
  
  public static String stringFromCookie(BasicClientCookie cookie) {
    StringBuilder sb = new StringBuilder();
    
    sb.append(cookie.getName().replaceAll(SEPERATOR, "\"" + SEPERATOR));
    sb.append(SEPERATOR);
    sb.append(cookie.getValue().replaceAll(SEPERATOR, "\"" + SEPERATOR));
    
    for(String attr : ATTRIB_KEYS) {
      sb.append(SEPERATOR);
      String val = cookie.getAttribute(attr);
      if (val != null) {
        sb.append(val);
      }
    }
    
    return sb.toString();
  }
  
  
  public static String stringFromCookies(List<Cookie> cookies) {
    String retVal = null;
    
    for (Cookie c : cookies) {
      if (c instanceof BasicClientCookie) {
        if (retVal == null) {
          retVal = stringFromCookie((BasicClientCookie) c);
        }
        else {
          retVal += COOKIE_SEPERATOR + stringFromCookie((BasicClientCookie) c); 
        }
      }
    }
    
    return retVal;
  }
}
